; NAME:
; AppendStartAndEnd_Mozart_NC
;
; PURPOSE:
; Reads in the given files and appends a point either end of each file. If there is no time point
; before or after then it replicates the start or end point. This over writes the inital files.
;
; CATEGORY:
; Regridding
;
; CALLING SEQUENCE:
; AppendStartAndEnd_Mozart_NC, Files
;
; INPUTS:
; Files
; ENDONLY = Set if you only want to append to the end of the files
; STARTONLY = Set if you only want to append to the start of the files
;
; OUTPUTS:
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;   Written Feb 27, 2014 by as13988
pro AppendStartAndEnd_Mozart_NC, Files, ENDONLY = EndOnly, STARTONLY = StartOnly, SPECIES = Species, TITLE = title, VARIABLE = Variable, UNITS = Units

  if keyword_set(SPECIES) eq 0 then SPECIES = 'CO2'
  
  if keyword_set(variable) eq 0 then variable = 'emissions'
  
  
  
  
  for i = 0L, N_elements(Files)-1 do begin
  
    ; Read in the file
    Read_MZT_File_Multi, FILES = Files[i], Data, FLUX_NCNAME = variable, DATESEC_NCNAME = 'time'
    
    gauge_flux_extract_file_info, files[i],  DimNames, DimSizes, VarInfo,FLUX_NCNAME = variable, DATESEC_NCNAME = 'date'
    
    flux_info = (varinfo[variable[0]])
    variable_long_name = (flux_info.attributes)[where(strmatch(flux_info.attname, 'long_name'))]
    
    lat = Data.lat
    lon = Data.lon
    InFlux = Data.flux
    InDate = Data.date
    InTime = Data.time
    
    if n_elements(size(InFlux, /DIMENSIONS)) gt  3 then begin
      Flag_3D = 1
      
      Read_MZT_File_Multi, FILES = Files[i], PS_Data, FLUX_NCNAME = "PS", DATESEC_NCNAME = 'time'
      InPS = PS_Data.Flux
      
    endif
    
    
    if keyword_set(ENDONLY) eq 0 then begin
      ; Find the time point before hand
      ; Read in the file before
      Before_i = i-1
      
      if Before_i ge 0 then begin ; Before file exists
      
        Read_MZT_File_Multi, FILES = Files[Before_i], BeforeData, FLUX_NCNAME = variable, DATESEC_NCNAME = 'time'
        
        if keyword_set(flag_3D) eq 1 then begin
          ; it's a 3D array
          StartFlux = (BeforeData.flux)[*,*,*,-1]
          startflux = reform(startflux, [size(startflux, /DIMENSIONS),1])
          
          Read_MZT_File_Multi, FILES = Files[Before_i], PS_BeforeData, FLUX_NCNAME = 'PS', DATESEC_NCNAME = 'time'
          
          StartPS = (PS_BeforeData.flux)[*,*,-1]
          
        endif else StartFlux = (BeforeData.flux)[*,*,-1] ; it's 2D array
        
        undefine, beforedata
        
        
      endif else begin ; before file does not exist
      
        if keyword_set(flag_3D) eq 1 then begin
          ; it's a 3D array
          StartFlux = Influx[*,*,*,0]
          startflux = reform(startflux, [size(startflux, /DIMENSIONS),1])
          
          StartPS = InPS[*,*,-1]
          
        endif else StartFlux = InFlux[*,*,0]
        
      endelse
      
      ;want it to put the date as 1/1/YYYY
      JD = mr_mozdate(InDate)
      Caldat, JD, Month, Day, year
      StartDate = mr_mozdate(double(Julday(Month[0], 1, Year[0])))
      StartTime = InTime[0] - (Day[0] -1) ; Deduct no days away from 1st of jan
      
      
    endif else begin
    
      ; Don't want to add the prior time point
      StartDate = !null
      StartTime = !null
      StartFlux = !null
      
    endelse
    
    
    if keyword_set(STARTONLY) eq 0 then begin
      ; Read in file after
      After_i = i+1
      
      if After_i le N_elements(Files)-1 then begin ; before file exists
      
        Read_MZT_File_Multi, FILES = Files[After_i], AfterData, FLUX_NCNAME = variable, DATESEC_NCNAME = 'time'
        
        EndDate = (AfterData.date)[0]
        EndTime = (AfterData.time)[0]
        
        if keyword_set(flag_3D) eq 1 then begin
          ; it's a 3D array
          EndFlux = (AfterData.flux)[*,*,*,0]
          Endflux = reform(endflux, [size(endflux, /DIMENSIONS),1])
          
          Read_MZT_File_Multi, FILES = Files[After_i], PS_AfterData, FLUX_NCNAME = 'PS', DATESEC_NCNAME = 'time'
          
          EndPS = (PS_AfterData.flux)[*,*,0]
          
        endif else EndFlux = (AfterData.flux)[*,*,0]
        
        undefine, AfterData
        
      endif else begin
      
        ; Need to fix the below to change the time step....
        JD = mr_mozdate(InDate)
        EndDate = mr_mozdate(double(JD[-1] +1))
        EndTime = InTime[-1] + (InTime[1] - InTime[0])
        
        
        if keyword_set(flag_3D) eq 1 then begin
          ; it's a 3D array
          EndFlux = Influx[*,*,*,-1]
          Endflux = reform(endflux, [size(endflux, /DIMENSIONS),1])
          
          EndPS = InPS[*,*,-1]
          
        endif else EndFlux = InFlux[*,*,-1]
        
      endelse
      
    endif else begin
    
      ; Don't want to add the posterior time point
      EndDate = !null
      EndTime = !null
      EndFlux = !null
    endelse
    
    
    undefine, data
    
    ; Combine the variables
    OutDate = [StartDate, InDate, EndDate]
    OutTime = [StartTime, InTime, EndTime]
    
    if keyword_set(flag_3d) then begin
      Outflux =  Concat(startflux, INflux, DIMENSION=3)
      Outflux = concat(outflux, endflux, DIMENSION=3)
    endif else OutFlux = [[[StartFlux]],[[ InFlux]],[[EndFlux]]]
    
    if keyword_set(flag_3D) eq 1 then OutPS = [[[StartPS]],[[ InPS]],[[EndPS]]]
    
    ; Split out Dir and file name
    FileName = FILE_BASENAME(Files[i])
    FileDir = File_DirName(Files[i]) + '/'
    
    If keyword_set(Flag_3d) then begin
      Read_MZT_File_Multi, FILES = Files[i], LevData, FLUX_NCNAME = 'lev', DATESEC_NCNAME = 'time'
      Read_MZT_File_Multi, FILES = Files[i], HyamData, FLUX_NCNAME = 'hyam', DATESEC_NCNAME = 'time'
      Read_MZT_File_Multi, FILES = Files[i], HybmData, FLUX_NCNAME = 'hybm', DATESEC_NCNAME = 'time'
      
       WRITE_MOZART_3D_NC, lat, lon,  levData.flux, hyamData.flux, hybmData.flux, OutPS, OutFlux, OutDate, $
        OutTime, SPECIES, FileName, FileDir, TITLE = Title, VARIABLE_NAME=variable, VARIABLE_LONG_NAME = variable_long_name, UNITS = Units
     endif else WRITE_MOZART_NC, lat, lon, OutFlux, OutDate, OutTime, SPECIES, FileName, FileDir, TITLE = Title, VARIABLE_NAME=variable, VARIABLE_long_name = variable_long_name, UNITS = Units
  endfor
  
  
  
  
end